/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @UploadMoreImageTask.java  2014年9月23日 下午4:03:22 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.BaseDataEntity;
import com.nineoldandroids.util.StringUtil;

public class UploadMoreImageTask extends AsyncTask<String, Void, ArrayList<String>> {
	int total;
	ArrayList<String> error = new ArrayList<String>();
	private UpLoadListener upLoadListener;

	public UpLoadListener getUpLoadListener() {
		return upLoadListener;
	}

	public void setUpLoadListener(UpLoadListener upLoadListener) {
		this.upLoadListener = upLoadListener;
	}

	@Override
	protected ArrayList<String> doInBackground(String... params) {
		ArrayList<String> strings = new ArrayList<String>();
		total = params.length;
		try {
			for (int i = 0; i < total; i++) {
				File file = new File(params[i]);
				if (file.exists()) {
					String string = upLoadTest(HttpUrl.getUrl(HttpUrl.UploadNormalImage), params[i]);
					if (!StringUtil.isEmpty(string)) {
						try {
							BaseDataEntity entity = (BaseDataEntity) JSON.parseObject(string,
									BaseDataEntity.class);
							if (entity != null) {
								if (entity.getState() == 1) {
									if (!StringUtil.isEmpty(entity.getData())) {
										strings.add(entity.getData());
									} else {
										error.add(params[i]);
									}
								} else {
									error.add(params[i]);
								}
							}
						} catch (Exception e) {

						}
					} else {
						error.add(params[i]);
					}
				} else {
					error.add(params[i]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return strings;
	}

	@Override
	protected void onPostExecute(ArrayList<String> result) {
		super.onPostExecute(result);
		if (result.size() > 0) {
			if (error.size() > total) {
				if (null != upLoadListener) {
					upLoadListener.UpLoadError(error);
				}
			}
			if (result.size() > 0) {
				if (null != upLoadListener) {
					upLoadListener.UpLoadResult(result);
				}
			} else {
				if (null != upLoadListener) {
					upLoadListener.UpLoadFail();
				}
			}
		} else {
			if (null != upLoadListener) {
				upLoadListener.UpLoadFail();
			}
		}
	}

	private String upLoadTest(String url, String filePath) {
		HttpClient httpclient = new DefaultHttpClient();
		String strResult = "";
		HttpPost httppost = new HttpPost(url);
		File file = new File(filePath);
		MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create(); // 文件传输
		try {
			mBuilder.addBinaryBody("file", new FileInputStream(file),
					ContentType.APPLICATION_OCTET_STREAM, ".jpg");
			 mBuilder.addTextBody("token", ShareCookie.getToken());
			httppost.setEntity(mBuilder.build());
			httppost.setHeader("User-Agent", "Tiangou-Android");
			HttpResponse response = httpclient.execute(httppost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(response.getEntity());
				return strResult;
			} else {
				return strResult;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strResult;
	}

	public interface UpLoadListener {
		void UpLoadError(ArrayList<String> notUpPath);

		void UpLoadResult(ArrayList<String> path);

		void UpLoadFail();
	}

}
